<template>
  <div class="department-container">
    <!-- 页面标题 -->
    <div class="page-header">
      <h1 class="page-title">部门管理</h1>
    </div>

    <!-- 添加部门区域 -->
    <div class="add-section">
      <div class="form-row">
        <input
          type="text"
          placeholder="请输入部门名称"
          class="form-input name-input"
          v-model="name"
        />
        <input
          type="text"
          placeholder="请输入部门编码"
          class="form-input code-input"
          v-model="code"
        />
        <button class="add-btn" @click="add">
          <span class="btn-icon">+</span>
          添加部门
        </button>
      </div>
    </div>

    <!-- 部门列表 -->
    <div class="department-list">
      <div class="list-header">
        <div class="header-item">部门编码</div>
        <div class="header-item">部门名称</div>
        <div class="header-item">操作</div>
      </div>

      <div v-if="list.length === 0" class="empty-state">
        <div class="empty-icon">🏢</div>
        <p>暂无部门数据</p>
      </div>

      <div v-else class="list-content">
        <div v-for="item in list" :key="item._id" class="list-item">
          <div class="item-cell code-cell">
            <span class="department-code">{{ item.code }}</span>
          </div>
          <div class="item-cell name-cell">
            <span class="department-name">{{ item.name }}</span>
          </div>
          <div class="item-cell actions">
            <button class="action-btn edit-btn" @click="Xg(item._id)">
              修改
            </button>
            <button class="action-btn delete-btn" @click="Sc(item._id)">
              删除
            </button>
          </div>
        </div>
      </div>
    </div>

    <!-- 编辑弹框 -->
    <div class="modal-overlay" v-show="flag" @click="flag = false">
      <div class="edit-section" @click.stop>
        <h3 class="modal-title">编辑部门</h3>
        <div class="form-group">
          <label class="form-label">部门名称</label>
          <input type="text" v-model="name1" placeholder="请输入部门名称" class="form-input" />
        </div>
        <div class="form-group">
          <label class="form-label">部门编码</label>
          <input type="text" v-model="code1" placeholder="请输入部门编码" class="form-input" />
        </div>
        <div class="modal-actions">
          <button class="modal-btn save-btn" @click="Bc(id)">保存</button>
          <button class="modal-btn cancel-btn" @click="flag = false">取消</button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import axios from 'axios'
export default {
  data() {
    return {
      list: [],
      name: '',
      code: '',
      name1: '',
      code1: '',
      id: null,
      flag: false,
    }
  },
  methods: {
    add() {
      axios
        .post(
          'http://47.94.6.186:3001/admin/department/create',
          {
            parentId: null,
            code: this.code,
            name: this.name,
            sort: 1,
            status: 1,
          },
          {
            headers: {
              token: localStorage.getItem('loginToken'),
            },
          },
        )
        .then((res) => {
          console.log(res.data.data)
          this.list.push(res.data.data)
          this.name = ''
          this.code = ''
        })
    },
    Sc(id) {
      axios
        .delete(`http://47.94.6.186:3001/admin/department/delete/${id}`, {
          headers: {
            token: localStorage.getItem('loginToken'),
          },
        })
        .then((res) => {
          console.log(res.data.data)
          this.list = this.list.filter((item) => item._id !== id)
        })
    },
    Xg(id) {
      this.flag = true
      this.id = id
      this.name1 = this.list.find((item) => item._id === id).name
      this.code1 = this.list.find((item) => item._id === id).code
    },
    Bc(id) {
      axios
        .put(
          `http://47.94.6.186:3001/admin/department/update/${id}`,
          {
            name: this.name1,
            code: this.code1,
          },
          {
            headers: {
              token: localStorage.getItem('loginToken'),
            },
          },
        )
        .then((res) => {
          console.log(res.data.data)
          console.log(res.data)
          alert('修改成功')
          this.flag = false
          // 更新本地列表中的数据
          const index = this.list.findIndex((item) => item._id === id)
          if (index !== -1) {
            this.list[index].name = this.name1
            this.list[index].code = this.code1
          }
        })
    },
  },
  mounted() {
    axios
      .get('http://47.94.6.186:3001/admin/department/list', {
        headers: {
          token: localStorage.getItem('loginToken'),
        },
      })
      .then((res) => {
        console.log(res.data.data)
        this.list = res.data.data
      })
  },
}
</script>
<style lang="css" scoped>
.department-container {
  padding: 20px;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  min-height: 100vh;
}

.page-header {
  background: white;
  padding: 25px;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  margin-bottom: 25px;
  text-align: center;
}

.page-title {
  font-size: 28px;
  color: #2c3e50;
  margin: 0;
  font-weight: 700;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.add-section {
  background: white;
  padding: 25px;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  margin-bottom: 25px;
}

.form-row {
  display: flex;
  gap: 15px;
  align-items: center;
  flex-wrap: wrap;
}

.form-input {
  padding: 12px 16px;
  border: 2px solid #e1e5e9;
  border-radius: 8px;
  font-size: 14px;
  outline: none;
  transition: all 0.3s ease;
  background: #fafbfc;
}

.form-input:focus {
  border-color: #667eea;
  background: white;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.name-input {
  flex: 1;
  min-width: 200px;
}

.code-input {
  flex: 1;
  min-width: 200px;
}

.add-btn {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  padding: 12px 24px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.3s ease;
  white-space: nowrap;
}

.add-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3);
}

.department-list {
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.list-header {
  display: grid;
  grid-template-columns: 200px 1fr 200px;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  padding: 20px 25px;
  font-weight: 600;
  color: #495057;
  border-bottom: 2px solid #dee2e6;
}

.header-item {
  display: flex;
  align-items: center;
  font-size: 15px;
}

.list-content {
  max-height: 500px;
  overflow-y: auto;
}

.list-item {
  display: grid;
  grid-template-columns: 200px 1fr 200px;
  padding: 20px 25px;
  border-bottom: 1px solid #f1f3f4;
  transition: all 0.3s ease;
  align-items: center;
}

.list-item:hover {
  background: linear-gradient(135deg, #f8f9ff 0%, #f0f4ff 100%);
  transform: translateX(5px);
}

.list-item:last-child {
  border-bottom: none;
}

.item-cell {
  display: flex;
  align-items: center;
}

.code-cell {
  font-family: 'Courier New', monospace;
}

.department-code {
  background: linear-gradient(135deg, #e3f2fd 0%, #bbdefb 100%);
  color: #1565c0;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 600;
  border: 1px solid #90caf9;
}

.department-name {
  color: #2c3e50;
  font-weight: 600;
  font-size: 15px;
}

.actions {
  gap: 10px;
  justify-content: flex-end;
}

.action-btn {
  padding: 8px 16px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: all 0.3s ease;
}

.edit-btn {
  background: linear-gradient(135deg, #fff3e0 0%, #ffe0b2 100%);
  color: #f57c00;
  border: 1px solid #ffcc02;
}

.edit-btn:hover {
  background: linear-gradient(135deg, #f57c00 0%, #ff9800 100%);
  color: white;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(245, 124, 0, 0.3);
}

.delete-btn {
  background: linear-gradient(135deg, #ffebee 0%, #ffcdd2 100%);
  color: #d32f2f;
  border: 1px solid #f44336;
}

.delete-btn:hover {
  background: linear-gradient(135deg, #d32f2f 0%, #f44336 100%);
  color: white;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(211, 47, 47, 0.3);
}

.btn-icon {
  font-size: 14px;
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #6c757d;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.empty-state p {
  font-size: 16px;
  color: #6c757d;
  margin: 0;
}

/* 弹框样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.6);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  backdrop-filter: blur(5px);
}

.edit-section {
  background: white;
  padding: 35px;
  border-radius: 16px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  width: 450px;
  max-width: 90vw;
  animation: modalSlideIn 0.3s ease-out;
}

@keyframes modalSlideIn {
  from {
    opacity: 0;
    transform: translateY(-50px) scale(0.9);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.modal-title {
  font-size: 20px;
  font-weight: 700;
  color: #2c3e50;
  margin: 0 0 25px 0;
  text-align: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.form-group {
  margin-bottom: 20px;
}

.form-label {
  display: block;
  font-size: 14px;
  font-weight: 600;
  color: #495057;
  margin-bottom: 8px;
}

.edit-section .form-input {
  width: 100%;
  box-sizing: border-box;
  padding: 14px 16px;
  font-size: 15px;
}

.modal-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  margin-top: 30px;
}

.modal-btn {
  padding: 12px 24px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  transition: all 0.3s ease;
  min-width: 80px;
}

.save-btn {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.save-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4);
}

.cancel-btn {
  background: #f8f9fa;
  color: #6c757d;
  border: 2px solid #dee2e6;
}

.cancel-btn:hover {
  background: #e9ecef;
  border-color: #adb5bd;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .department-container {
    padding: 15px;
  }

  .form-row {
    flex-direction: column;
    align-items: stretch;
  }

  .name-input,
  .code-input {
    min-width: auto;
  }

  .list-header,
  .list-item {
    grid-template-columns: 1fr;
    gap: 15px;
    text-align: left;
  }

  .item-cell {
    padding: 8px 0;
    border-bottom: 1px solid #f1f3f4;
    flex-direction: column;
    align-items: flex-start;
  }

  .item-cell:last-child {
    border-bottom: none;
    flex-direction: row;
    justify-content: flex-start;
  }

  .actions {
    justify-content: flex-start;
  }

  .edit-section {
    width: 95%;
    padding: 25px;
  }

  .modal-actions {
    flex-direction: column;
  }

  .modal-btn {
    width: 100%;
  }
}

@media (max-width: 480px) {
  .page-title {
    font-size: 24px;
  }

  .action-btn {
    padding: 6px 12px;
    font-size: 11px;
  }

  .btn-icon {
    font-size: 12px;
  }
}
</style>
